<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Rbac;

use App\Controller\Admin\AdminController;
use App\Repositories\AdminRepository;
use App\Repositories\RbacMenuRepository;
use App\Repositories\RbacRoleRepository;
use App\Request\Rbac\StoreRbacMenuRequest;
use App\Request\Rbac\StoreRoleRequest;
use App\Request\StoreAdminRequest;
use Hyperf\Di\Annotation\Inject;

class MenuController extends AdminController{

    public function select()
    {
        $top = (int)$this->request->input('top',1);//是否展示顶级
        $line = (int)$this->request->input('line',1);//是否展示树形线
        $return = RbacMenuRepository::getInstance()->view($top,$line);
        return $this->response->success($return);
    }

    public function all(){
        $search=[];
        $datas = RbacMenuRepository::getInstance()->getList('*',$search,false,[[
            'field'=>'listorder',
            'sort'=>'asc'
        ]]);
        return $this->response->success($datas);
    }

    public function list(){
        $parent_id = $this->request->input('parent_id',0);
        $search = [
            ['field'=>'parent_id','value'=>$parent_id]
        ];
        $orderBy= [
            [
                'field' => 'listorder',
                'sort' => 'asc'
            ],
            [
                'field' => 'id',
                'sort' => 'asc'
            ]
        ];
        $datas = RbacMenuRepository::getInstance()->getList('*',$search,['limit'=>10000],$orderBy);
        foreach ($datas['result'] as $key=>$data){
            if($data['rgt']-$data['lft']>1){
                $datas['result'][$key]['children']=[];
            }
        }
        return $this->response->success($datas);
    }

    public function store(StoreRbacMenuRequest $request){
        $params = $request->all();
        $params['is_visable'] = isset($params['is_visable'])&&$params['is_visable']=='true'?1:0;
        $result = RbacMenuRepository::getInstance()->store($params);
        if($result['success']){
            return $this->response->success($result['data']);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function detail(){

    }

    public function delete(){

    }

    public function changeField()
    {
        $field = $this->request->input('field');
        $value = $this->request->input('value',false);
        $id = $this->request->input('id');
        $update =[
            $field=>$value=='true'?1:0
        ];
        $where=[
            [
                'field'=>'id',
                'value'=>$id
            ]
        ];
        $return = AdminRepository::getInstance()->update($update,$where);
        return $this->response->success([]);
    }
}